package com.sandama.bbs.fragment;


import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.Bind;
import butterknife.ButterKnife;
import com.sandama.bbs.R;
import com.sandama.bbs.activity.MainActivity;
import com.sandama.bbs.adapter.GroupGamesAdapter;
import com.sandama.bbs.model.Group;
import com.sandama.bbs.util.DividerItemDecoration;

/**
 * Author:TongxiJi
 * Function:
 * Create Date:十月08,2015
 */
public class BBSMainFragment extends Fragment {

    public static final String TAG = "BBSMainFragment";
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";
    @Bind(R.id.recylerView)
    RecyclerView recylerView;
    @Bind(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;


    private GroupGamesAdapter groupGamesAdapter;

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static BBSMainFragment newInstance(int sectionNumber) {
        BBSMainFragment fragment = new BBSMainFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public BBSMainFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        ButterKnife.bind(this, rootView);

        initListener();
        initData();
        return rootView;
    }

    private void initData() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recylerView.setLayoutManager(linearLayoutManager);
        recylerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));

        fullList();
    }

    private void fullList() {
        if (groupGamesAdapter == null) {
            groupGamesAdapter = new GroupGamesAdapter();
            recylerView.setAdapter(groupGamesAdapter);
        }
        Group group = ((MainActivity) getActivity()).getGroups().get(getArguments().getInt(ARG_SECTION_NUMBER));
        groupGamesAdapter.setGroupList(group.getGames());
        groupGamesAdapter.notifyDataSetChanged();
    }

    private void initListener() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                fullList();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}